/**
 * 初始化详情对话框
 */
var ClmRoomInfoDlg = {
    clmRoomInfoData : {}
};

/**
 * 清除数据
 */
ClmRoomInfoDlg.clearData = function() {
    this.clmRoomInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ClmRoomInfoDlg.set = function(key, val) {
    this.clmRoomInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ClmRoomInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ClmRoomInfoDlg.close = function() {
    parent.layer.close(window.parent.ClmRoom.layerIndex);
}


/**
 * 收集数据
 */
ClmRoomInfoDlg.collectData = function() {
    this
    .set('roomId')
    .set('siteId')
    .set('teacherId')
    .set('number')
    .set('subject')
    .set('startDate')
    .set('liveType')
    .set('publicScope')
    .set('schoolId')
    .set('stageId')
    .set('gradeId')
    .set('dictGradeId')
    .set('classId')
    .set('subjectId')
    .set('courseCode')
    .set('teacherName')
    .set('schoolName')
    .set('teacherToken')
    .set('studentToken')
    .set('studentClientToken')
    .set('invalidDate')
    .set('assistantToken')
    .set('speakerInfo')
    .set('scheduleInfo')
    .set('webJoin')
    .set('clientJoin')
    .set('teacherJoinUrl')
    .set('studentJoinUrl')
    .set('description')
    .set('duration')
    .set('uiMode')
    .set('uiColor')
    .set('scene')
    .set('uiWindow')
    .set('uiVideo')
    .set('mobileMode')
    .set('upgrade')
    .set('sec')
    .set('realtime')
    .set('maxAttendees')
    .set('createUserId')
    .set('createDate')
    .set('beLogin')
    .set('imageUrl')
    .set('subjectName')
    .set('gradeName')
    .set('className')
    .set('beDeleted')
    .set('thirdSubject')
    .set('beCheck')
    .set('upUrl')
    .set('avatar')
    .set('starTeacherId')
    .set('starTeacherId_old')
    .set('column_ids')
    .set('courseware_selected')
    .set('courseware_unselected');
    
    var ids=$("#column_ids").val();
    var tmp=ids.split(",");
    for(var i=0;i<tmp.length;i++){
    	var key="column"+tmp[i];
    	var oldKey="columnOld"+tmp[i];
    	this.set(key);
    	this.set(oldKey);
    }
}

/**
 * 提交添加
 */
ClmRoomInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/clmRoom/add", function(data){
        Feng.success("添加成功!");
        window.parent.ClmRoom.table.refresh();
        ClmRoomInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.clmRoomInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
ClmRoomInfoDlg.editSubmit = function() {
	//判断是否选择名师
	var cId=$("#starTeacherColumnId").val();
	var isSelectedMS=$("#"+cId+" option:selected").val();
	var MS=$("#starTeacherId option:selected").val();
	if('0' == isSelectedMS){
		if(''==MS){
			Feng.info("请选择名师");
			return;
		}
	}
    this.clearData();
    chk();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/clmRoom/update", function(data){
        Feng.success("修改成功!");
        window.parent.ClmRoom.table.refresh();
        ClmRoomInfoDlg.close();
        
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    
    ajax.set("isSelectedMS",isSelectedMS);
    ajax.set(this.clmRoomInfoData);
    ajax.start();
}

/**
 * 提交公用修改
 */
ClmRoomInfoDlg.utilEditSubmit = function() {

	//判断是否选择名师
	var cId=$("#starTeacherColumnId").val();
	var isSelectedMS=$("#"+cId+" option:selected").val();
	var MS=$("#starTeacherId option:selected").val();
	if('0' == isSelectedMS){
		if(''==MS){
			Feng.info("请选择名师");
			return;
		}
	}
	
    this.clearData();
    chk();
    this.collectData();
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/clmRoom/update", function(data){
        Feng.success("修改成功!");
        parent.roomTableRefresh();
        //ClmRoomInfoDlg.close();
        parent.roomClose();
        //parent.layer.close(window.parent.ClmRoom.layerIndex);
        
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    //console.log(this.clmRoomInfoData.toString());
    ajax.set("isSelectedMS",isSelectedMS);
    ajax.set(this.clmRoomInfoData);
    ajax.start();
}

/**
 * 关闭公用对话框
 */
ClmRoomInfoDlg.utilClose = function() {
	parent.roomClose();
}


//获取录播件选项
function chk(){ 
	var obj=document.getElementsByName('courseware'); //选择所有name="'courseware'"的对象，返回数组 
	//取到对象数组后，我们来循环检测它是不是被选中 
	var s=''; 
	var n='';
	for(var i=0; i<obj.length; i++){ 
		if(obj[i].checked){ 
			s+=obj[i].value+','; //如果选中，将value添加到变量s中 
		}else{
			n+=obj[i].value+',';
		} 
	}
	$("#courseware_selected").val(s);
	$("#courseware_unselected").val(n);
} 

//初始化专题
function columnInit(){
	 var ids=$("#column_ids").val();
	    var tmp=ids.split(",");
	    for(var i=0;i<tmp.length;i++){
	    	var key="#column"+tmp[i];
	    	var oldKey="#columnOld"+tmp[i];
	    	$(key).val($(oldKey).val());
	    }
}


$(function() {

	// 初始化头像上传
    var avatarUp = new $WebUpload("avatar");
    avatarUp.setUploadBarId("progressBar");
    avatarUp.init();
    
    //初始化专题
    columnInit();
    //初始化名师
    $("#starTeacherId").val($("#starTeacherId_old").val());
    
    
});
